<?php

namespace App\Kuafu\Shop\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class PrinterModel extends BaseModel
{
    use HasFactory, KfSoftDeletes, KfUid;

    protected $table = 'printers';

    protected $fillable = [
        'name',
        'shopId',
        'deviceId',
        'printerType',
        'deviceType',
        'PrinterIp',
        'PrinterPort',
        'status',
        'brandId',
    ];


    /**
     * 新增店铺
     * @param string $name
     * @param int $shopId
     * @param int $brandId
     * @param int $deviceId
     * @param int $printerType
     * @param int $deviceType
     * @param string $printerIp
     * @param int $printerPort
     * @param int $status
     * @return array
     * @throws ShopException
     */
    public static function addData(
        string $name,
        int $shopId,
        int $brandId,
        int $deviceId,
        int $printerType,
        int $deviceType,
        string $printerIp,
        int $printerPort,
        int $status
    ) {
        try {
            $printer = new PrinterModel();
            $printer->name        = $name;
            $printer->shopId      = $shopId;
            $printer->brandId     = $brandId;
            $printer->deviceId    = $deviceId;
            $printer->printerType = $printerType;
            $printer->deviceType  = $deviceType;
            $printer->printerIp   = $printerIp;
            $printer->printerPort = $printerPort;
            $printer->status      = $status;
            $printer->save();
            return ['id'=>$printer->id];
        } catch (\Exception $exception) {
           throw new ShopException(ShopCodeEnum::ERR_SHOP_INSERT_PRINTER, '', $exception);
        }
    }

    public static function delData(int $id): int
    {
        return self::query()->where('id', $id)->update(['isDeleted' => 1]);
    }
}
